package cn.quang.search.toolkit.component.items.impl;

import cn.quang.search.toolkit.component.AbstractSearchItem;
import cn.quang.search.toolkit.component.constants.Constants;
import cn.quang.search.toolkit.enums.DataSourceTypeEnum;
import cn.quang.search.toolkit.enums.SearchOperator;
import cn.quang.search.toolkit.provider.entity.KeyOperatorValue;
import cn.quang.search.toolkit.provider.entity.QuerySlice;

import java.util.Arrays;
import java.util.Collections;

/**
 * ES查询用地图搜索
 */
public class GeoSearchItem extends AbstractSearchItem {

    private final double longitude;
    private final double latitude;
    private final String distance;

    public GeoSearchItem(DataSourceTypeEnum dsType, String col, double longitude, double latitude, String distance){
        super(dsType, col, SearchOperator.Equal);
        this.longitude = longitude;
        this.latitude = latitude;
        this.distance = distance;
    }

    @Override
    public QuerySlice getQuerySlice() {

        if(dsType == DataSourceTypeEnum.ES) {
            String sqlTemplate = String.format("GEO_DISTANCE(%s, %s, %s, %s)", Constants.FIELD, Constants.VALUE, Constants.VALUE, Constants.VALUE);
            return QuerySlice.of(
                    sqlTemplate,
                    KeyOperatorValue.of(col, Collections.singletonList(this.col), Collections.emptyList(), Arrays.asList(distance, longitude, latitude))
            );
        }

        throw new IllegalArgumentException("不支持的数据源!");
    }
}
